package com.daybook.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daybook.mapper.CategoryMapper;
import com.daybook.pojo.Category;
import com.daybook.service.CategoryService;
import com.daybook.util.PageHelp;
@Service
public class CategoryServiceImpl implements CategoryService{
	@Autowired
	private CategoryMapper categoryMapper;
	/**
	 * 获取分页记录
	 * @param pageNow:当前页码，若为null值，则为1
	 * @return 返回page和list集合
	 */

	public PageHelp getLimitPageList(Integer pageNow,String cname) {
	    int totalCount=categoryMapper.getCount(cname);//获取总的记录数
	    List<Category> stuList=new ArrayList<Category>();
	    PageHelp page=null;
	    if(pageNow!=null){
	        page=new PageHelp(totalCount, pageNow);
	        page.setPageSize(10);
	        stuList=categoryMapper.selectByPage(page.getStartPos(), page.getPageSize(),cname);//从startPos开始，获取pageSize条数据
	    }else{
	        page=new PageHelp(totalCount, 1);//初始化pageNow为1
	        page.setPageSize(10);
	        stuList=categoryMapper.selectByPage(page.getStartPos(), page.getPageSize(),cname);//从startPos开始，获取pageSize条数据
	    }
	    page.setList(stuList);
	    
	    return page;
	}
	//查询所有类别
	public List<Category> selectAllCategory(){
		return categoryMapper.selectAllCategory();
	}
	//类别添加
		public int addCategory(Category category){
			int result=-1;
			result=categoryMapper.selectByCname(category.getCname());
			//resule=0时说明没有要插入的类别，当前类别可以添加
			if(result==0){
				result=categoryMapper.add(category);
			}
			return result;
		}
		//类别删除
		public int delCategory(int cid){
			int result=-1;
			result=categoryMapper.del(cid);
			return result;
		}
		//验证是否存在
		public int getOldCategory(String cname){
			int result=-1;
			result=categoryMapper.selectByCname(cname);
			return result;
		}
	//类别信息更新
	public int updateCategory(Category category){
		int result=categoryMapper.update(category);
		return result;
	}
}
